library(tidyverse)
library(lmtest)
library(sandwich)
library(stargazer)
library(readxl)
library(lubridate) 


#A2
#######################################################################



df <- read_xlsx(("data\\raw\\parlgov\\parlgov-election.xlsx")) %>%
  filter(country_name != "Denmark")

#add seat proportion variable
df$seat_share <- df$seats / df$seats_total



#analysis
#no fixed effects
reg.1 <- lm(vote_share ~ coalition_prev*far_right, data = df)
summary(reg.1)

se.reg.1 <- coeftest(reg.1, vcov = vcovCL, cluster = ~far_right)
se.reg.1

#country fixed effects

reg.2 <- lm(vote_share ~ factor(country_name) + coalition_prev*far_right, data = df)
summary(reg.2)

se.reg.2 <- coeftest(reg.2, vcov = vcovCL, cluster = ~far_right)
se.reg.2


#table
stargazer(se.reg.1, se.reg.2,
          column.labels = c("1", "2"),
          keep = c("coalition_prev", "far_right", 
                   "coalition_prev:far_right", "Constant"),
          covariate.labels = c("Previous Coalition Membership", "Far-Right", "Previous Coalition Membership $\times$ Far-Right", "Constant"),
          dep.var.labels = c("Seat Share"),
          notes.label = "Clustered standard errors in parentheses",
          add.lines = c("N", "Adj. R-squared", "F-statistic", "Fixed Effects"),
          table.placement = "H"
)

#comparison with other party families

parl.party <- read_xlsx("data\\raw\\parlgov\\parlgov-stable.xlsx", sheet = "party") %>%
  filter(c(country_name == "Austria" | country_name == "Hungary" | country_name == "Italy" | 
             country_name == "Netherlands" | country_name == "Norway" | country_name == "Poland" | country_name == "Switzerland"))

df$family_id<- parl.party$family_id[match(df$party_id, parl.party$party_id)]


#remove other party family
df <- df %>%
  filter(family_id %in% c("14", "11", "6",
                         "3", "26", "40"))


#set FR as base category
df$family_id <- relevel(factor(df$family_id), ref = 6) 

reg.3 <- lm(vote_share ~ factor(country_name) + coalition_prev*factor(family_id), data = df)
summary(reg.3)

se.reg.3 <- coeftest(reg.3, vcov = vcovCL, cluster = ~far_right)
se.reg.3


#A3
###############################################
#summary statistics of covariates


#load data
df <- read.csv(("data\\clean\\roe2023data.csv"))


balance <- df %>% group_by(coalition.current) %>% summarise(mean.age = mean(agea),
                                                            sd.age = sd(agea),
                                                            mean.eduyrs = mean(eduyrs),
                                                            sd.eduyrs = sd(eduyrs),
                                                            mean.hincta = mean(hinctnta),
                                                            sd.hincta = sd(hinctnta),
                                                            mean.lrscale = mean(lrscale),
                                                            sd.lrscale = sd(lrscale),
                                                            mean.polintr = mean(polintr),
                                                            sd.polintr = sd(polintr)
)
knitr::kable(head(balance), "latex")

df %>%
  group_by(prtvtfam)%>%
  summarise(count = n())


#histogram of satisfaction variable and months variable
hist(df1$stfgov,
     main = "",
     xlab = "Satisfaction with Government")

hist(df1$months.last.elec,
     main = "",
     xlab = "Months Since Last Election")

#summary of respondents by month
months.summary <- df %>%
  group_by(months.last.elec)%>%
  summarise(count = n())


#summary of respondents by month AND party family
months.summary <- df %>%
  group_by(months.last.elec, prtvtfam.cond)%>%
  summarise(count = n())

##############################################################################################
#full results 


##########################################################################################
#Government Satisfaction DV
#load data
df1 <- read.csv(("data\\clean\\roe2023data.csv"))

#set FR as base category
df1$prtvtfam <- relevel(factor(df1$prtvtfam), ref = 4) 

#no fixed effects, no controls
reg.1.basic <- lm(stfgov ~ coalition.current + prtvtfam + coalition.current*prtvtfam, 
                  data = df1)

summary(reg.1.basic)

#robust standard errors
se.reg.1.basic <- coeftest(reg.1.basic, vcov = vcovHC)
se.reg.1.basic


#controls, no fixed effects
reg.1.cont <- lm(stfgov ~ coalition.current + prtvtfam + coalition.current*prtvtfam + agea + blgetmg + eduyrs +
                   gndr + hinctnta + lrscale + mbtru + seat_share, 
                 data = df1)

summary(reg.1.cont)

#robust standard errors
se.reg.1.cont <- coeftest(reg.1.cont, vcov = vcovHC)
se.reg.1.cont




#country and year fixed effects, controls (full model)
#set FR as base category

reg.1.fixed <- lm(stfgov ~ coalition.current + prtvtfam + coalition.current*prtvtfam + agea + blgetmg + eduyrs +
                    gndr + hinctnta + lrscale + mbtru + seat_share + 
                    factor(cntry) + factor(inwyys), 
                  data = df1)


summary(reg.1.fixed)

#robust standard errors
se.reg.1.fixed <- coeftest(reg.1.fixed, vcov = vcovHC)
se.reg.1.fixed


stargazer(se.reg.1.basic, se.reg.1.cont, se.reg.1.fixed,
          dep.var.labels = c("Satisfaction with Government"),
#          covariate.labels = c("Government", "Christian Dem.", "Communist",
#                               "Conservative", "Liberal", "Social Dem.", "Seat Share",
#                               "Government $\times$ Christian Dem.", "Government $\times$ Communist ",
#                               "Government $\times$ Conservative", "Government $\times$ Liberal", 
#                               "Government $\times$ Social Dem.",
#                               "Constant"),
          notes.label = "Robust standard errors in parentheses",
          add.lines = c("N", "Adj. R-squared", "F-statistic", "Control Variables","Fixed Effects"),
          table.placement = "H",
          font.size = "tiny")


#Party Closeness DV

#load data
df1 <- read.csv(("data\\clean\\roe2023data.csv"))


#set FR as base category
df1$prtvtfam <- relevel(factor(df1$prtvtfam), ref = 4) 


#DV = probability that the party you feel close to is the same one you voted for
reg.2.basic <- glm(clsprty.same ~ f.coalition.current + factor(prtvtfam) + f.coalition.current*factor(prtvtfam),
                   data = df1,
                   family = binomial(link = "logit"))
summary(reg.2.basic)

#robust standard errors
se.reg.2.basic <- coeftest(reg.2.basic, vcov = vcovHC)
se.reg.2.basic
logLik(reg.2.basic)

reg.2.cont <- glm(clsprty.same ~ f.coalition.current + factor(prtvtfam) + f.coalition.current*factor(prtvtfam) +
                    agea + blgetmg + eduyrs +
                    gndr + hinctnta + lrscale + 
                    mbtru + seat_share,
                  data = df1,
                  family = binomial(link = "logit"))

summary(reg.2.cont)

#robust standard errors
se.reg.2.cont <- coeftest(reg.2.cont, vcov = vcovHC)
se.reg.2.cont
logLik(reg.2.cont)







reg.2.full <- glm(clsprty.same ~ f.coalition.current + factor(prtvtfam) + f.coalition.current*factor(prtvtfam) +
                    agea + blgetmg + eduyrs +
                    gndr + hinctnta + lrscale + 
                    mbtru + seat_share +
                    f.cntry + f.inwyys, 
                  data = df1,
                  family = binomial(link = "logit"))
summary(reg.2.full)
#robust standard errors
se.reg.2.full <- coeftest(reg.2.full, vcov = vcovHC)
se.reg.2.full
logLik(reg.2.full)

stargazer(se.reg.2.basic, se.reg.2.cont, se.reg.2.full,
          dep.var.labels = c("Party Closeness"),
#          covariate.labels = c("Government", "Christian Dem.", "Communist", "Conservative",
#                               "Liberal", "Social Dem.", "Seat Share", 
#                               "Government $\times$ Christian Dem.", "Government $\times$ Communist", 
#                               "Government $\times$ Conservative", "Government $\times$ Liberal", "Government $\times$ Social Dem.", 
#                               "Constant"),
          notes.label = "Standard errors in parentheses",
          table.placement = "H")





#ROBUSTNESS FULL RESULTS
#############################################################

#load data
df1 <- read.csv(("data\\clean\\roe2023data.csv"))


#set FR as base category
df1$prtvtfam <- relevel(factor(df1$prtvtfam), ref = 4) 
df1$f.prtvtfam <- relevel(factor(df1$f.prtvtfam), ref = 4) 

#Triple interaction term
#country and year fixed effects, controls (full model)
reg.3.fixed <- lm(stfgov ~ factor(coalition.current) + factor(prtvtfam) + months.last.elec+ factor(coalition.current)*factor(prtvtfam)*months.last.elec + 
                    agea + blgetmg + eduyrs +
                    gndr + hinctnta + lrscale + mbtru + seat_share + 
                    factor(cntry) + factor(inwyys), 
                  data = df1)
summary(reg.3.fixed)

#robust standard errors
se.reg.3.fixed <- coeftest(reg.3.fixed, vcov = vcovHC)
se.reg.3.fixed

#probability that the party you feel close to is the same one you voted for
reg4.full <- glm(clsprty.same ~ f.coalition.current + f.prtvtfam + months.last.elec +
                   f.coalition.current*f.prtvtfam*months.last.elec +
                   agea + blgetmg + eduyrs +
                   gndr + hinctnta + lrscale + 
                   mbtru + seat_share +
                   f.cntry + f.inwyys, 
                 data = df1,
                 family = binomial(link = "logit"))
summary(reg4.full)

stargazer(se.reg.3.fixed, reg4.full,
          dep.var.labels = c("Satisfaction with Government", "Party Closeness"),
          #          covariate.labels = c("Coalition", "Christian Dem.", "Liberal",
          #                               "Social Dem.", "Communist", "Conservative", "Coalition1", "Christian Dem.1", "Liberal1",
          #                               "Social Dem.1", "Communist1", "Conservative1", "Months", "Seat Share", "Govt Effectiveness",
          #                               "Coalition $\times$ Christian Dem.", "Coalition $\times$ Liberal",
          #                               "Coalition $\times$ Social Dem.", "Coalition $\times$ Communist", 
          #                               "Coalition $\times$ Conservative", "Coalition $\times$ Months", "Coalition $\times$ Months",
          #                               "Christian Dem $\times$ Months", "Liberal $\times$ Months", "Social Dem. $\times$ Months",
          #                               "Communist $\times$ Months", "Conservative $\times$ Months"),
          notes.label = "",
          add.lines = c("N", "Adj. R-squared", "F-statistic", 
                        "Log-Liklihood", "Control Variables","Fixed Effects"),
          table.placement = "H",
          font.size = "tiny")



##############################################################
#exclluding specific countries
#NO SWITZERLAND
df.no.ch <- df1 %>%
  filter(cntry != "CH")

#country and year fixed effects, controls (full model)
reg.no.ch <- lm(stfgov ~ factor(coalition.current) + factor(prtvtfam) + factor(coalition.current)*factor(prtvtfam) + agea + blgetmg + eduyrs +
                  gndr + hinctnta + lrscale + mbtru  + seat_share + 
                  factor(cntry) + factor(inwyys), 
                data = df.no.ch)
summary(reg.no.ch)

#robust standard errors
se.reg.no.ch <- coeftest(reg.no.ch, vcov = vcovHC)
se.reg.no.ch

#NO HUNGARY OR POLAND
df.no.hupl <- df1 %>%
  filter(cntry != "HU") %>%
  filter(cntry != "PL")

#country and year fixed effects, controls (full model)
reg.no.hupl <- lm(stfgov ~ factor(coalition.current) + factor(prtvtfam) + factor(coalition.current)*factor(prtvtfam) + agea + blgetmg + eduyrs +
                    gndr + hinctnta + lrscale + mbtru + seat_share  + 
                    factor(cntry) + factor(inwyys), 
                  data = df.no.hupl)
summary(reg.no.hupl)

#robust standard errors
se.reg.no.hupl <- coeftest(reg.no.hupl, vcov = vcovHC)
se.reg.no.hupl

#NO SWITZERLAND, HUNGARY, OR POLAND
df.no.all <- df1 %>%
  filter(cntry != "HU") %>%
  filter(cntry != "PL") %>%
  filter(cntry != "CH")

#country and year fixed effects, controls (full model)
reg.no.all <- lm(stfgov ~ factor(coalition.current) + factor(prtvtfam) + factor(coalition.current)*factor(prtvtfam) + agea + blgetmg + eduyrs +
                   gndr + hinctnta + lrscale + mbtru + seat_share + 
                   factor(cntry) + factor(inwyys), 
                 data = df.no.all)
summary(reg.no.all)



#robust standard errors
se.reg.no.all <- coeftest(reg.no.all, vcov = vcovHC)
se.reg.no.all

stargazer(se.reg.no.ch, se.reg.no.hupl, se.reg.no.all,
          dep.var.labels = c("Satisfaction with Government"),
          #          covariate.labels = c("Coalition", "Christian Dem.", "Liberal",
          #                               "Social Dem.", "Communist", "Conservative", "Seat Share", "Govt Effectiveness",
          #                               "Coalition $\times$ Christian Dem.", "Coalition $\times$ Liberal",
          #                               "Coalition $\times$ Social Dem.", "Coalition $\times$ Communist", 
          #                               "Coalition $\times$ Conservative",
          #                               "Constant"),
          notes.label = "Robust standard errors in parentheses",
          add.lines = c("N", "Adj. R-squared", "F-statistic", "Control Variables","Fixed Effects"),
          table.placement = "H",
          font.size = "tiny")




#probability that the party you feel close to is the same one you voted for
reg2.no.ch <- glm(clsprty.same ~ f.coalition.current + f.prtvtfam + f.coalition.current*f.prtvtfam +
                    agea + blgetmg + eduyrs +
                    gndr + hinctnta + lrscale + 
                    mbtru + seat_share +
                    f.cntry + f.inwyys, 
                  data = df.no.ch,
                  family = binomial(link = "logit"))
summary(reg2.no.ch)

reg2.no.hupl <- glm(clsprty.same ~ f.coalition.current + f.prtvtfam + f.coalition.current*f.prtvtfam +
                      agea + blgetmg + eduyrs +
                      gndr + hinctnta + lrscale + 
                      mbtru + seat_share +
                      f.cntry + f.inwyys, 
                    data = df.no.hupl,
                    family = binomial(link = "logit"))
summary(reg2.no.hupl)

reg2.no.all <- glm(clsprty.same ~ f.coalition.current + f.prtvtfam + f.coalition.current*f.prtvtfam +
                     agea + blgetmg + eduyrs +
                     gndr + hinctnta + lrscale + 
                     mbtru + seat_share +
                     f.cntry + f.inwyys, 
                   data = df.no.all,
                   family = binomial(link = "logit"))
summary(reg2.no.all)

stargazer(reg2.no.ch, reg2.no.hupl, reg2.no.all,
          keep = c("f.coalition.current",
                   "f.prtvtfam",
                   "seat_share",
                   "govt_eff",
                   "Constant"),
          dep.var.labels = c("Party Closeness"),
          #          covariate.labels = c("Coalition", "Christian Dem.", "Liberal",
          #                               "Social Dem.", "Communist", "Conservative", "Seat Share", "Govt Effectiveness",
          #                               "Coalition $\times$ Christian Dem.", "Coalition $\times$ Liberal",
          #                               "Coalition $\times$ Social Dem.", "Coalition $\times$ Communist", 
          #                               "Coalition $\times$ Conservative", 
          #                               "Constant"),
          notes.label = "Standard errors in parentheses",
          table.placement = "H")



